package com.heima.behavior.service.impl;

import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author: itheima
 * @create: 2022-07-09 15:23
 */
@Service
public class D {

    @Autowired
    private A a;
    @Autowired
    private B b;
    @Autowired
    private C c;

    /**
     * 远程调用其他三个服务Feign接口
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public ResponseResult queryData() throws ExecutionException, InterruptedException {
        //从线程池中获取一个子线程 //1s
        Future<Integer> future1 = a.queryDianzan(1L);
        //从线程池中获取一个子线程 //1s
        Future<Integer> future2 = b.queryShoucang(1L);
        //从线程池中获取一个子线程  //1s
        Future<Integer> future3 = c.queryNotLike(1L);


        //同步调用需要3秒后得到结果
        //加入多线程异步方法后，1秒后得到三个远程接口响应结果
        Integer aVal = future1.get();
        Integer bVal = future2.get();
        Integer cVal = future3.get();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("aVal", aVal);
        map.put("bVal", bVal);
        map.put("cVal", cVal);
        return ResponseResult.okResult(map);

    }
}
